
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class FamilySearchResult extends JFrame{

    public FamilySearchResult(String userid, Object familyID) {

        SQLOperation sql = new SQLOperation();

        setTitle("Search Result");
        Container container=getContentPane();
        setLayout(new BorderLayout());

        JPanel titlePanel=new JPanel();

        JLabel title=new JLabel(familyID+" Information");
        title.setFont(new Font("",Font.BOLD,20));

        titlePanel.add(title);

        add(titlePanel,BorderLayout.NORTH);

        String[] familyAttributes={"familyID","fName","address"};
        Object[][] detailInfo=sql.search("Family",familyAttributes,"familyID",familyID);

        String[] userAttributes={"uuID"};
        Object[][] familyMemberInfo=sql.search("User",userAttributes,"Family_familyID",familyID);

        int count=familyMemberInfo.length;

        JPanel info=new JPanel(new GridLayout(4,1,5,5));
        for(int i=0;i<3;i++){
            info.add(new JLabel(familyAttributes[i]+"  :  "+detailInfo[0][i]+"\n",JLabel.CENTER));
        }
        info.add(new JLabel("Family Members: "+count,JLabel.CENTER));

        add(info,BorderLayout.CENTER);




        JButton back=new JButton("Back");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });

        JButton modify=new JButton("Modify it");
        modify.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // MODIFY

                setVisible(false);
                new FamilyModify(userid,familyID);


            }
        });

        JButton delete=new JButton("Delete it");
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int deleteOperation=JOptionPane.showConfirmDialog
                        (null, "Confirm Delete?","Confirm",JOptionPane.YES_NO_OPTION);

                if(deleteOperation==JOptionPane.YES_OPTION){

                    sql.modify("Family","familyID",familyID,"shanchu",0);

                    sql.modify("User","Family_familyID",familyID,"Family_familyID","0");

                    JOptionPane.showMessageDialog
                            (null, "Delete Complete!","Success",JOptionPane.INFORMATION_MESSAGE);

                    setVisible(false);


                }




            }
        });
        JPanel buttons=new JPanel();
        buttons.add(back);
        buttons.add(modify);
        buttons.add(delete);

        add(buttons,BorderLayout.SOUTH);






        setSize(500,300);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);

    }
}
